package com.srxz.personnelsystem.system.service.impl;

import com.srxz.personnelsystem.system.entity.LoginInfo;
import com.srxz.personnelsystem.system.entity.UserInfo;
import com.srxz.personnelsystem.system.mapper.converter.LoginInfoMapper;
import com.srxz.personnelsystem.system.mapper.converter.UserInfoMapper;
import com.srxz.personnelsystem.system.mapper.dto.LoginInfoDTO;
import com.srxz.personnelsystem.system.mapper.dto.UserInfoDTO;
import com.srxz.personnelsystem.system.repository.LoginInfoRepository;
import com.srxz.personnelsystem.system.repository.UserInfoRepository;
import com.srxz.personnelsystem.system.service.SysService;
import com.srxz.personnelsystem.system.service.mapimpl.UserMapperServiceImpl;
import com.srxz.personnelsystem.util.BackMessage;
import com.srxz.personnelsystem.util.jwt.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @作者：wgchao
 * @时间：19:50 2019/1/22 0022
 * @用途：
 **/
@Service
@Transactional
public class SysServiceImpl extends UserMapperServiceImpl implements SysService {
    @Autowired
    private LoginInfoRepository loginInfoRepository;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 用户登录
     * @param uname
     * @param password
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public BackMessage login(String uname, String password) {
        String pwdMD5 = DigestUtils.md5DigestAsHex(password.getBytes());
        LoginInfo loginInfo = loginInfoRepository.findFirstByUnameAndDeleteTimeIsNull(uname);

        //错误处理
        if(loginInfo == null)
            return BackMessage.error("登录账户不存在");
        if(!pwdMD5.equals(loginInfo.getPassword()))
            return BackMessage.error("登录密码错误");

        //获取登录用户详细信息
        UserInfo userInfo = userInfoRepository.getOne(loginInfo.getUserId());
        if(userInfo == null || userInfo.getDeleteTime()!=null)
            return BackMessage.error("登录用户已失效");
        UserInfoDTO userInfoDTO = userInfoMapper.dto(userInfo);
        extendLoginInfoDTO(userInfoDTO);

        //获取JWT 并返回并返回给前端
        String token = JWTUtil.sign(loginInfo.getUname(), loginInfo.getPassword());
        if(token == null){
            return BackMessage.error("获取认证信息失败");
        }

        return BackMessage.success().putData("user", userInfoDTO).putData("token", token);
    }

    @Override
    public BackMessage saveLoginInfo(LoginInfoDTO loginInfoDTO) {
        if(loginInfoDTO.getType() == null)
            return BackMessage.error("账户类型缺失");
        LoginInfo loginInfo = loginInfoMapper.entity(loginInfoDTO);
        if(loginInfoDTO.getType() == 0){//系统账户

        }else if(loginInfoDTO.getType() == 1){//个人账户
            if(StringUtils.isEmpty(loginInfoDTO.getIdcard())){
                return BackMessage.error("员工信息缺失");
            }
            UserInfo userInfo = userInfoRepository.findFirstByIdcardAndDeleteTimeIsNullAndStatusNot(loginInfoDTO.getIdcard(), -1);
            if(userInfo == null){
                return BackMessage.error("不存在可用的员工信息");
            }
            loginInfo.setName(userInfo.getName());
            loginInfo.setUserId(userInfo.getId());
        }else{
            return BackMessage.error("账户类型错误");
        }

        loginInfo.setCreateTime(new Date());

        loginInfo = loginInfoRepository.save(loginInfo);

        return BackMessage.success().putData("logininfo", loginInfoMapper.dto(loginInfo));
    }
}
